/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.administrator.xiaoxiang.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.entity.ShopingcartEntity;
import com.example.administrator.xiaoxiang.view.NumberAddSubView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * Created by YOLANDA on 2016/7/22.
 */
public class ShopCardAdapter extends SwipeMenuAdapter<ShopCardAdapter.MyViewHolder>  {
    public  interface OnMyButtonClickListener{
        void addNum(int num,long _id);
        void subNum(int num,long _id);
    }
    private OnMyButtonClickListener myButtonClickListener;
    private List<ShopingcartEntity> data;
    private Context context;
    public ShopCardAdapter(Context context,List<ShopingcartEntity> data,OnMyButtonClickListener myButtonClickListener){
        this.context =context;
        this.data = data;
        this.myButtonClickListener = myButtonClickListener;
    }
    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shop,null);
        return view;
    }

    @Override
    public MyViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new MyViewHolder(realContentView);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final ShopingcartEntity shopingcartEntity = data.get(position);
        holder.new_price.setText("￥"+shopingcartEntity.getPrice_new()+"x"+shopingcartEntity.getCount()+"期");
        holder.old_price.setText(shopingcartEntity.getPrice_old());
        holder.old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.title.setText(shopingcartEntity.getTitle());
        Glide.with(context).load(shopingcartEntity.getPhotoUrl()).placeholder(R.drawable.home_goods_small_nothing).into(holder.imageView);
        holder.numberAddSubView.setValue(parseInt(shopingcartEntity.getNum()));
        holder.numberAddSubView.setOnButtonClickListener(new NumberAddSubView.OnButtonClickListener() {
            int num;
            @Override
            public void onButtonAddClick(View view, int value) {
                num = Integer.parseInt(shopingcartEntity.getNum()) + 1;
                Log.e("YY", "onButtonAddClick: "+num );
                myButtonClickListener.addNum(num,shopingcartEntity.get_id());
                double v = Double.parseDouble(shopingcartEntity.getPrice_new())*num;
                holder.new_price.setText("￥"+v+"x"+shopingcartEntity.getCount()+"期");
//                holder.numberAddSubView.setValue(value);
            }

            @Override
            public void onButtonSubClick(View view, int value) {
                num = Integer.parseInt(shopingcartEntity.getNum()) - 1;
                if (num<=0)
                    num = 0;
                myButtonClickListener.subNum(num,shopingcartEntity.get_id());
                double v = Double.parseDouble(shopingcartEntity.getPrice_new())*num;
                holder.new_price.setText("￥"+v+"x"+shopingcartEntity.getCount()+"期");

//                holder.numberAddSubView.setValue(value);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView  imageView;
        TextView title;
        TextView old_price;
        NumberAddSubView numberAddSubView;
        TextView new_price;
        Button jiesuan;
    public MyViewHolder(View view) {
        super(view);
        imageView = (ImageView) view.findViewById(R.id.card_image);
        title = (TextView) view.findViewById(R.id.tv_title_shop);
        old_price = (TextView) view.findViewById(R.id.half_text);
        numberAddSubView = (NumberAddSubView) view.findViewById(R.id.numbersub);
        new_price = (TextView) view.findViewById(R.id.new_price);
        jiesuan = (Button) view.findViewById(R.id.jiesuan);
    }
}
}
